{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np # 数据处理最重要的模块\n",
    "import pandas as pd # 数据处理最重要的模块\n",
    "import scipy.stats as stats # 统计模块\n",
    "import scipy\n",
    "# import pymysql  # 导入数据库模块\n",
    "from datetime import datetime # 时间模块\n",
    "import statsmodels.formula.api as smf  # OLS regression\n",
    "import statsmodels.api as sm\n",
    "# import pyreadr # read RDS file\n",
    "from matplotlib import style\n",
    "import matplotlib.pyplot as plt  # 画图模块\n",
    "import matplotlib.dates as mdates\n",
    "from matplotlib.font_manager import FontProperties # 作图中文\n",
    "from pylab import mpl\n",
    "#mpl.rcParams['font.sans-serif'] = ['SimHei']\n",
    "#plt.rcParams['font.family'] = 'Times New Roman'\n",
    "#输出矢量图 渲染矢量图\n",
    "%matplotlib inline\n",
    "%config InlineBackend.figure_format = 'svg'\n",
    "from IPython.core.interactiveshell import InteractiveShell # jupyter运行输出的模块\n",
    "#显示每一个运行结果\n",
    "InteractiveShell.ast_node_interactivity = 'all'\n",
    "#设置行不限制数量\n",
    "#pd.set_option('display.max_rows',None)\n",
    "#设置列不限制数量\n",
    "pd.set_option('display.max_columns', None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>uid</th>\n",
       "      <th>id</th>\n",
       "      <th>provincecode</th>\n",
       "      <th>birthday</th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "      <th>areaid</th>\n",
       "      <th>provincename</th>\n",
       "      <th>cityname</th>\n",
       "      <th>areaname</th>\n",
       "      <th>instalments_num</th>\n",
       "      <th>instalments_amount</th>\n",
       "      <th>shop_address</th>\n",
       "      <th>trans_date</th>\n",
       "      <th>taobaostartdate</th>\n",
       "      <th>taobaoenddate</th>\n",
       "      <th>taobaodealno</th>\n",
       "      <th>apptimes</th>\n",
       "      <th>deal</th>\n",
       "      <th>apply_request_sum</th>\n",
       "      <th>apply_reject_sum</th>\n",
       "      <th>loan_offer_sum</th>\n",
       "      <th>repay_fail_sum</th>\n",
       "      <th>max_default_days</th>\n",
       "      <th>default</th>\n",
       "      <th>province</th>\n",
       "      <th>nominalrates</th>\n",
       "      <th>default30</th>\n",
       "      <th>alipaybalance</th>\n",
       "      <th>yuebaobalance</th>\n",
       "      <th>huabeiamount</th>\n",
       "      <th>huabeibalance</th>\n",
       "      <th>creditlevelasbuyer</th>\n",
       "      <th>tencentscore</th>\n",
       "      <th>gaodescore</th>\n",
       "      <th>numbercontact</th>\n",
       "      <th>numbercontact20s</th>\n",
       "      <th>highcontact</th>\n",
       "      <th>highcontact20s</th>\n",
       "      <th>numbercontacttotal</th>\n",
       "      <th>default_firstmonth_0</th>\n",
       "      <th>default_firstmonth_30</th>\n",
       "      <th>default_firstmonth_60</th>\n",
       "      <th>delaydate_max</th>\n",
       "      <th>default_max_0</th>\n",
       "      <th>default_max_30</th>\n",
       "      <th>default_max_60</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>72146187</td>\n",
       "      <td>1200345737</td>\n",
       "      <td>43</td>\n",
       "      <td>19951203</td>\n",
       "      <td>24</td>\n",
       "      <td>False</td>\n",
       "      <td>431222</td>\n",
       "      <td>湖南省</td>\n",
       "      <td>怀化市</td>\n",
       "      <td>沅陵县</td>\n",
       "      <td>12</td>\n",
       "      <td>603000</td>\n",
       "      <td>香花桥街道大盈路361号</td>\n",
       "      <td>2017/12/2</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>hunan</td>\n",
       "      <td>0.494169</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>60.200000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>101101513</td>\n",
       "      <td>1200456775</td>\n",
       "      <td>32</td>\n",
       "      <td>19880607</td>\n",
       "      <td>31</td>\n",
       "      <td>False</td>\n",
       "      <td>320382</td>\n",
       "      <td>江苏省</td>\n",
       "      <td>徐州市</td>\n",
       "      <td>邳州市</td>\n",
       "      <td>12</td>\n",
       "      <td>270000</td>\n",
       "      <td>上海市上海市普陀区普陀区真如镇曹杨路2083号</td>\n",
       "      <td>2019/2/18</td>\n",
       "      <td>2018/12/14</td>\n",
       "      <td>2019/1/1</td>\n",
       "      <td>40</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>False</td>\n",
       "      <td>jiangsu</td>\n",
       "      <td>0.359378</td>\n",
       "      <td>False</td>\n",
       "      <td>1.94</td>\n",
       "      <td>0.00</td>\n",
       "      <td>50</td>\n",
       "      <td>50.00</td>\n",
       "      <td>1.0</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>47</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>-3.0</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>98608810</td>\n",
       "      <td>1200502203</td>\n",
       "      <td>42</td>\n",
       "      <td>19960503</td>\n",
       "      <td>23</td>\n",
       "      <td>False</td>\n",
       "      <td>420821</td>\n",
       "      <td>湖北省</td>\n",
       "      <td>荆门市</td>\n",
       "      <td>京山县</td>\n",
       "      <td>12</td>\n",
       "      <td>398000</td>\n",
       "      <td>上海市上海市普陀区宜君路92号</td>\n",
       "      <td>2019/4/22</td>\n",
       "      <td>2018/12/10</td>\n",
       "      <td>2019/4/22</td>\n",
       "      <td>44</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>hubei</td>\n",
       "      <td>0.204553</td>\n",
       "      <td>NaN</td>\n",
       "      <td>179.11</td>\n",
       "      <td>0.00</td>\n",
       "      <td>300</td>\n",
       "      <td>2.44</td>\n",
       "      <td>3.0</td>\n",
       "      <td>60.200000</td>\n",
       "      <td>0.156100</td>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>23</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>96300664</td>\n",
       "      <td>1200424426</td>\n",
       "      <td>51</td>\n",
       "      <td>20000620</td>\n",
       "      <td>19</td>\n",
       "      <td>True</td>\n",
       "      <td>510824</td>\n",
       "      <td>四川省</td>\n",
       "      <td>广元市</td>\n",
       "      <td>苍溪县</td>\n",
       "      <td>12</td>\n",
       "      <td>300000</td>\n",
       "      <td>浙江省宁波市保税区庐山西路158号1幢1-14号C区</td>\n",
       "      <td>2018/12/2</td>\n",
       "      <td>2018/6/12</td>\n",
       "      <td>2018/12/2</td>\n",
       "      <td>85</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>False</td>\n",
       "      <td>sichuan</td>\n",
       "      <td>0.204560</td>\n",
       "      <td>False</td>\n",
       "      <td>0.00</td>\n",
       "      <td>36.32</td>\n",
       "      <td>1000</td>\n",
       "      <td>647.92</td>\n",
       "      <td>161.0</td>\n",
       "      <td>53.875000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>0</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>-23.0</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>105585598</td>\n",
       "      <td>1200512684</td>\n",
       "      <td>32</td>\n",
       "      <td>19940122</td>\n",
       "      <td>25</td>\n",
       "      <td>False</td>\n",
       "      <td>321324</td>\n",
       "      <td>江苏省</td>\n",
       "      <td>宿迁市</td>\n",
       "      <td>泗洪县</td>\n",
       "      <td>12</td>\n",
       "      <td>378000</td>\n",
       "      <td>江苏省南京市秦淮区秦淮路11号欧尚超市</td>\n",
       "      <td>2019/5/11</td>\n",
       "      <td>2018/11/11</td>\n",
       "      <td>2019/4/30</td>\n",
       "      <td>32</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>False</td>\n",
       "      <td>jiangsu</td>\n",
       "      <td>0.252571</td>\n",
       "      <td>False</td>\n",
       "      <td>0.00</td>\n",
       "      <td>66.96</td>\n",
       "      <td>500</td>\n",
       "      <td>500.00</td>\n",
       "      <td>29.0</td>\n",
       "      <td>35.000000</td>\n",
       "      <td>0.189800</td>\n",
       "      <td>8</td>\n",
       "      <td>7</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>46</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>0.0</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4995</th>\n",
       "      <td>97368947</td>\n",
       "      <td>1200582090</td>\n",
       "      <td>41</td>\n",
       "      <td>19870821</td>\n",
       "      <td>32</td>\n",
       "      <td>False</td>\n",
       "      <td>411528</td>\n",
       "      <td>河南省</td>\n",
       "      <td>信阳市</td>\n",
       "      <td>息县</td>\n",
       "      <td>12</td>\n",
       "      <td>458000</td>\n",
       "      <td>江苏省南京市浦口区明发滨江新城308栋1032室</td>\n",
       "      <td>2019/8/30</td>\n",
       "      <td>2019/3/1</td>\n",
       "      <td>2019/8/30</td>\n",
       "      <td>33</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>henan</td>\n",
       "      <td>0.204559</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "      <td>3327.91</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>146.0</td>\n",
       "      <td>53.000000</td>\n",
       "      <td>0.305604</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>88</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4996</th>\n",
       "      <td>83278392</td>\n",
       "      <td>1200369370</td>\n",
       "      <td>34</td>\n",
       "      <td>19980626</td>\n",
       "      <td>21</td>\n",
       "      <td>False</td>\n",
       "      <td>341221</td>\n",
       "      <td>安徽省</td>\n",
       "      <td>阜阳市</td>\n",
       "      <td>临泉县</td>\n",
       "      <td>12</td>\n",
       "      <td>130000</td>\n",
       "      <td>上海市上海市浦东新区北艾路1432号</td>\n",
       "      <td>2018/5/16</td>\n",
       "      <td>2017/11/23</td>\n",
       "      <td>2018/5/16</td>\n",
       "      <td>105</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>True</td>\n",
       "      <td>anhui</td>\n",
       "      <td>0.358954</td>\n",
       "      <td>False</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>500</td>\n",
       "      <td>253.57</td>\n",
       "      <td>17.0</td>\n",
       "      <td>58.111111</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>10</td>\n",
       "      <td>9</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>88</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>4.0</td>\n",
       "      <td>True</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4997</th>\n",
       "      <td>45196409</td>\n",
       "      <td>1200379786</td>\n",
       "      <td>34</td>\n",
       "      <td>19950412</td>\n",
       "      <td>24</td>\n",
       "      <td>False</td>\n",
       "      <td>340321</td>\n",
       "      <td>安徽省</td>\n",
       "      <td>蚌埠市</td>\n",
       "      <td>怀远县</td>\n",
       "      <td>12</td>\n",
       "      <td>589000</td>\n",
       "      <td>上海市上海市闵行区浦江镇康华路杜行宿舍一楼超市对面</td>\n",
       "      <td>2018/7/15</td>\n",
       "      <td>2018/1/23</td>\n",
       "      <td>2018/7/10</td>\n",
       "      <td>35</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>anhui</td>\n",
       "      <td>0.213772</td>\n",
       "      <td>NaN</td>\n",
       "      <td>24.24</td>\n",
       "      <td>0.80</td>\n",
       "      <td>70</td>\n",
       "      <td>70.00</td>\n",
       "      <td>11.0</td>\n",
       "      <td>70.571429</td>\n",
       "      <td>0.241720</td>\n",
       "      <td>7</td>\n",
       "      <td>6</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>122</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4998</th>\n",
       "      <td>74566336</td>\n",
       "      <td>1200594221</td>\n",
       "      <td>43</td>\n",
       "      <td>19760702</td>\n",
       "      <td>43</td>\n",
       "      <td>True</td>\n",
       "      <td>433022</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>260000</td>\n",
       "      <td>浙江省宁波市鄞州区咸祥中路118号</td>\n",
       "      <td>2019/9/20</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>False</td>\n",
       "      <td>hunan</td>\n",
       "      <td>0.359846</td>\n",
       "      <td>False</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>60.000000</td>\n",
       "      <td>0.228332</td>\n",
       "      <td>16</td>\n",
       "      <td>16</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>72</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>0.0</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4999</th>\n",
       "      <td>90999009</td>\n",
       "      <td>1200390400</td>\n",
       "      <td>41</td>\n",
       "      <td>19990312</td>\n",
       "      <td>20</td>\n",
       "      <td>True</td>\n",
       "      <td>411527</td>\n",
       "      <td>河南省</td>\n",
       "      <td>信阳市</td>\n",
       "      <td>淮滨县</td>\n",
       "      <td>12</td>\n",
       "      <td>139000</td>\n",
       "      <td>上海市上海市浦东新区川沙路4759号</td>\n",
       "      <td>2018/8/29</td>\n",
       "      <td>2018/3/18</td>\n",
       "      <td>2018/8/29</td>\n",
       "      <td>73</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>henan</td>\n",
       "      <td>0.359367</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>700</td>\n",
       "      <td>427.33</td>\n",
       "      <td>9.0</td>\n",
       "      <td>60.200000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>True</td>\n",
       "      <td>True</td>\n",
       "      <td>36</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5000 rows × 47 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            uid          id  provincecode  birthday  age  gender  areaid  \\\n",
       "0      72146187  1200345737            43  19951203   24   False  431222   \n",
       "1     101101513  1200456775            32  19880607   31   False  320382   \n",
       "2      98608810  1200502203            42  19960503   23   False  420821   \n",
       "3      96300664  1200424426            51  20000620   19    True  510824   \n",
       "4     105585598  1200512684            32  19940122   25   False  321324   \n",
       "...         ...         ...           ...       ...  ...     ...     ...   \n",
       "4995   97368947  1200582090            41  19870821   32   False  411528   \n",
       "4996   83278392  1200369370            34  19980626   21   False  341221   \n",
       "4997   45196409  1200379786            34  19950412   24   False  340321   \n",
       "4998   74566336  1200594221            43  19760702   43    True  433022   \n",
       "4999   90999009  1200390400            41  19990312   20    True  411527   \n",
       "\n",
       "     provincename cityname areaname  instalments_num  instalments_amount  \\\n",
       "0             湖南省      怀化市      沅陵县               12              603000   \n",
       "1             江苏省      徐州市      邳州市               12              270000   \n",
       "2             湖北省      荆门市      京山县               12              398000   \n",
       "3             四川省      广元市      苍溪县               12              300000   \n",
       "4             江苏省      宿迁市      泗洪县               12              378000   \n",
       "...           ...      ...      ...              ...                 ...   \n",
       "4995          河南省      信阳市       息县               12              458000   \n",
       "4996          安徽省      阜阳市      临泉县               12              130000   \n",
       "4997          安徽省      蚌埠市      怀远县               12              589000   \n",
       "4998          NaN      NaN      NaN                6              260000   \n",
       "4999          河南省      信阳市      淮滨县               12              139000   \n",
       "\n",
       "                    shop_address trans_date taobaostartdate taobaoenddate  \\\n",
       "0                   香花桥街道大盈路361号  2017/12/2             NaN           NaN   \n",
       "1        上海市上海市普陀区普陀区真如镇曹杨路2083号  2019/2/18      2018/12/14      2019/1/1   \n",
       "2                上海市上海市普陀区宜君路92号  2019/4/22      2018/12/10     2019/4/22   \n",
       "3     浙江省宁波市保税区庐山西路158号1幢1-14号C区  2018/12/2       2018/6/12     2018/12/2   \n",
       "4            江苏省南京市秦淮区秦淮路11号欧尚超市  2019/5/11      2018/11/11     2019/4/30   \n",
       "...                          ...        ...             ...           ...   \n",
       "4995    江苏省南京市浦口区明发滨江新城308栋1032室  2019/8/30        2019/3/1     2019/8/30   \n",
       "4996          上海市上海市浦东新区北艾路1432号  2018/5/16      2017/11/23     2018/5/16   \n",
       "4997   上海市上海市闵行区浦江镇康华路杜行宿舍一楼超市对面  2018/7/15       2018/1/23     2018/7/10   \n",
       "4998           浙江省宁波市鄞州区咸祥中路118号  2019/9/20             NaN           NaN   \n",
       "4999          上海市上海市浦东新区川沙路4759号  2018/8/29       2018/3/18     2018/8/29   \n",
       "\n",
       "      taobaodealno  apptimes  deal  apply_request_sum  apply_reject_sum  \\\n",
       "0                0         1     0                  0                 0   \n",
       "1               40         1     1                  0                 0   \n",
       "2               44         1     0                  0                 0   \n",
       "3               85         1     1                  0                 0   \n",
       "4               32         1     1                  0                 0   \n",
       "...            ...       ...   ...                ...               ...   \n",
       "4995            33         1     0                  4                 4   \n",
       "4996           105         1     1                  0                 0   \n",
       "4997            35         1     0                  1                 0   \n",
       "4998             0         1     1                  0                 0   \n",
       "4999            73         1     0                  0                 0   \n",
       "\n",
       "      loan_offer_sum  repay_fail_sum  max_default_days default province  \\\n",
       "0                NaN             NaN               NaN     NaN    hunan   \n",
       "1                NaN             NaN               0.0   False  jiangsu   \n",
       "2                NaN             NaN               NaN     NaN    hubei   \n",
       "3                NaN             NaN               0.0   False  sichuan   \n",
       "4                NaN             NaN               0.0   False  jiangsu   \n",
       "...              ...             ...               ...     ...      ...   \n",
       "4995             1.0             0.0               NaN     NaN    henan   \n",
       "4996             NaN             NaN               4.0    True    anhui   \n",
       "4997             0.0             0.0               NaN     NaN    anhui   \n",
       "4998             0.0             0.0               0.0   False    hunan   \n",
       "4999             NaN             NaN               NaN     NaN    henan   \n",
       "\n",
       "      nominalrates default30  alipaybalance  yuebaobalance  huabeiamount  \\\n",
       "0         0.494169       NaN           0.00           0.00             0   \n",
       "1         0.359378     False           1.94           0.00            50   \n",
       "2         0.204553       NaN         179.11           0.00           300   \n",
       "3         0.204560     False           0.00          36.32          1000   \n",
       "4         0.252571     False           0.00          66.96           500   \n",
       "...            ...       ...            ...            ...           ...   \n",
       "4995      0.204559       NaN           0.00        3327.91             0   \n",
       "4996      0.358954     False           0.00           0.00           500   \n",
       "4997      0.213772       NaN          24.24           0.80            70   \n",
       "4998      0.359846     False           0.00           0.00             0   \n",
       "4999      0.359367       NaN           0.00           0.00           700   \n",
       "\n",
       "      huabeibalance  creditlevelasbuyer  tencentscore  gaodescore  \\\n",
       "0              0.00                 NaN     60.200000    0.192094   \n",
       "1             50.00                 1.0     48.000000    0.192094   \n",
       "2              2.44                 3.0     60.200000    0.156100   \n",
       "3            647.92               161.0     53.875000    0.192094   \n",
       "4            500.00                29.0     35.000000    0.189800   \n",
       "...             ...                 ...           ...         ...   \n",
       "4995           0.00               146.0     53.000000    0.305604   \n",
       "4996         253.57                17.0     58.111111    0.192094   \n",
       "4997          70.00                11.0     70.571429    0.241720   \n",
       "4998           0.00                 NaN     60.000000    0.228332   \n",
       "4999         427.33                 9.0     60.200000    0.192094   \n",
       "\n",
       "      numbercontact  numbercontact20s  highcontact  highcontact20s  \\\n",
       "0                 0                 0        False           False   \n",
       "1                 5                 5         True            True   \n",
       "2                 6                 4         True            True   \n",
       "3                 0                 0        False           False   \n",
       "4                 8                 7         True            True   \n",
       "...             ...               ...          ...             ...   \n",
       "4995              5                 5         True            True   \n",
       "4996             10                 9         True            True   \n",
       "4997              7                 6         True            True   \n",
       "4998             16                16         True            True   \n",
       "4999              5                 4         True            True   \n",
       "\n",
       "      numbercontacttotal default_firstmonth_0 default_firstmonth_30  \\\n",
       "0                      0                  NaN                   NaN   \n",
       "1                     47                False                 False   \n",
       "2                     23                  NaN                   NaN   \n",
       "3                      0                False                 False   \n",
       "4                     46                False                 False   \n",
       "...                  ...                  ...                   ...   \n",
       "4995                  88                  NaN                   NaN   \n",
       "4996                  88                False                 False   \n",
       "4997                 122                  NaN                   NaN   \n",
       "4998                  72                False                 False   \n",
       "4999                  36                  NaN                   NaN   \n",
       "\n",
       "     default_firstmonth_60  delaydate_max default_max_0 default_max_30  \\\n",
       "0                      NaN            NaN           NaN            NaN   \n",
       "1                    False           -3.0         False          False   \n",
       "2                      NaN            NaN           NaN            NaN   \n",
       "3                    False          -23.0         False          False   \n",
       "4                    False            0.0         False          False   \n",
       "...                    ...            ...           ...            ...   \n",
       "4995                   NaN            NaN           NaN            NaN   \n",
       "4996                 False            4.0          True          False   \n",
       "4997                   NaN            NaN           NaN            NaN   \n",
       "4998                 False            0.0         False          False   \n",
       "4999                   NaN            NaN           NaN            NaN   \n",
       "\n",
       "     default_max_60  \n",
       "0               NaN  \n",
       "1             False  \n",
       "2               NaN  \n",
       "3             False  \n",
       "4             False  \n",
       "...             ...  \n",
       "4995            NaN  \n",
       "4996          False  \n",
       "4997            NaN  \n",
       "4998          False  \n",
       "4999            NaN  \n",
       "\n",
       "[5000 rows x 47 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rawdata = pd.read_csv('Lecture 1 Data.csv')\n",
    "rawdata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "      <th>instalments_amount</th>\n",
       "      <th>nominalrates</th>\n",
       "      <th>tencentscore</th>\n",
       "      <th>gaodescore</th>\n",
       "      <th>highcontact</th>\n",
       "      <th>deal</th>\n",
       "      <th>default</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>24</td>\n",
       "      <td>False</td>\n",
       "      <td>603000</td>\n",
       "      <td>0.494169</td>\n",
       "      <td>60.200000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>False</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>31</td>\n",
       "      <td>False</td>\n",
       "      <td>270000</td>\n",
       "      <td>0.359378</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23</td>\n",
       "      <td>False</td>\n",
       "      <td>398000</td>\n",
       "      <td>0.204553</td>\n",
       "      <td>60.200000</td>\n",
       "      <td>0.156100</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>19</td>\n",
       "      <td>True</td>\n",
       "      <td>300000</td>\n",
       "      <td>0.204560</td>\n",
       "      <td>53.875000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>False</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>25</td>\n",
       "      <td>False</td>\n",
       "      <td>378000</td>\n",
       "      <td>0.252571</td>\n",
       "      <td>35.000000</td>\n",
       "      <td>0.189800</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4995</th>\n",
       "      <td>32</td>\n",
       "      <td>False</td>\n",
       "      <td>458000</td>\n",
       "      <td>0.204559</td>\n",
       "      <td>53.000000</td>\n",
       "      <td>0.305604</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4996</th>\n",
       "      <td>21</td>\n",
       "      <td>False</td>\n",
       "      <td>130000</td>\n",
       "      <td>0.358954</td>\n",
       "      <td>58.111111</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4997</th>\n",
       "      <td>24</td>\n",
       "      <td>False</td>\n",
       "      <td>589000</td>\n",
       "      <td>0.213772</td>\n",
       "      <td>70.571429</td>\n",
       "      <td>0.241720</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4998</th>\n",
       "      <td>43</td>\n",
       "      <td>True</td>\n",
       "      <td>260000</td>\n",
       "      <td>0.359846</td>\n",
       "      <td>60.000000</td>\n",
       "      <td>0.228332</td>\n",
       "      <td>True</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4999</th>\n",
       "      <td>20</td>\n",
       "      <td>True</td>\n",
       "      <td>139000</td>\n",
       "      <td>0.359367</td>\n",
       "      <td>60.200000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>True</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5000 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      age  gender  instalments_amount  nominalrates  tencentscore  gaodescore  \\\n",
       "0      24   False              603000      0.494169     60.200000    0.192094   \n",
       "1      31   False              270000      0.359378     48.000000    0.192094   \n",
       "2      23   False              398000      0.204553     60.200000    0.156100   \n",
       "3      19    True              300000      0.204560     53.875000    0.192094   \n",
       "4      25   False              378000      0.252571     35.000000    0.189800   \n",
       "...   ...     ...                 ...           ...           ...         ...   \n",
       "4995   32   False              458000      0.204559     53.000000    0.305604   \n",
       "4996   21   False              130000      0.358954     58.111111    0.192094   \n",
       "4997   24   False              589000      0.213772     70.571429    0.241720   \n",
       "4998   43    True              260000      0.359846     60.000000    0.228332   \n",
       "4999   20    True              139000      0.359367     60.200000    0.192094   \n",
       "\n",
       "      highcontact  deal default  \n",
       "0           False     0     NaN  \n",
       "1            True     1   False  \n",
       "2            True     0     NaN  \n",
       "3           False     1   False  \n",
       "4            True     1   False  \n",
       "...           ...   ...     ...  \n",
       "4995         True     0     NaN  \n",
       "4996         True     1    True  \n",
       "4997         True     0     NaN  \n",
       "4998         True     1   False  \n",
       "4999         True     0     NaN  \n",
       "\n",
       "[5000 rows x 9 columns]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "age                      0\n",
       "gender                   0\n",
       "instalments_amount       0\n",
       "nominalrates             3\n",
       "tencentscore             0\n",
       "gaodescore               0\n",
       "highcontact              0\n",
       "deal                     0\n",
       "default               2795\n",
       "dtype: int64"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = rawdata[['age', 'gender','instalments_amount','nominalrates','tencentscore','gaodescore','highcontact','deal','default']]\n",
    "data\n",
    "#检查是否有缺失值\n",
    "missing = data.isnull().sum()\n",
    "missing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可见，利率、买家信用分、违约是否均有空缺值，考虑到数据的宝贵，对不同研究问题采用不同的子数据表进行回归分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "age                     int64\n",
      "gender                   bool\n",
      "instalments_amount      int64\n",
      "nominalrates          float64\n",
      "tencentscore          float64\n",
      "gaodescore            float64\n",
      "highcontact              bool\n",
      "deal                    int64\n",
      "default                object\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "#检查数据格式，对于布尔型数据进行转换\n",
    "print(data.dtypes)\n",
    "#注意 default一列数据格式是object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_5045/3499703449.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  data['gender'] = data['gender'].astype(int)\n",
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_5045/3499703449.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  data['highcontact'] = data['highcontact'].astype(int)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "      <th>instalments_amount</th>\n",
       "      <th>nominalrates</th>\n",
       "      <th>tencentscore</th>\n",
       "      <th>gaodescore</th>\n",
       "      <th>highcontact</th>\n",
       "      <th>deal</th>\n",
       "      <th>default</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>24</td>\n",
       "      <td>0</td>\n",
       "      <td>603000</td>\n",
       "      <td>0.494169</td>\n",
       "      <td>60.200000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "      <td>270000</td>\n",
       "      <td>0.359378</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23</td>\n",
       "      <td>0</td>\n",
       "      <td>398000</td>\n",
       "      <td>0.204553</td>\n",
       "      <td>60.200000</td>\n",
       "      <td>0.156100</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>19</td>\n",
       "      <td>1</td>\n",
       "      <td>300000</td>\n",
       "      <td>0.204560</td>\n",
       "      <td>53.875000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>25</td>\n",
       "      <td>0</td>\n",
       "      <td>378000</td>\n",
       "      <td>0.252571</td>\n",
       "      <td>35.000000</td>\n",
       "      <td>0.189800</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4995</th>\n",
       "      <td>32</td>\n",
       "      <td>0</td>\n",
       "      <td>458000</td>\n",
       "      <td>0.204559</td>\n",
       "      <td>53.000000</td>\n",
       "      <td>0.305604</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4996</th>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "      <td>130000</td>\n",
       "      <td>0.358954</td>\n",
       "      <td>58.111111</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4997</th>\n",
       "      <td>24</td>\n",
       "      <td>0</td>\n",
       "      <td>589000</td>\n",
       "      <td>0.213772</td>\n",
       "      <td>70.571429</td>\n",
       "      <td>0.241720</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4998</th>\n",
       "      <td>43</td>\n",
       "      <td>1</td>\n",
       "      <td>260000</td>\n",
       "      <td>0.359846</td>\n",
       "      <td>60.000000</td>\n",
       "      <td>0.228332</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4999</th>\n",
       "      <td>20</td>\n",
       "      <td>1</td>\n",
       "      <td>139000</td>\n",
       "      <td>0.359367</td>\n",
       "      <td>60.200000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5000 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      age  gender  instalments_amount  nominalrates  tencentscore  gaodescore  \\\n",
       "0      24       0              603000      0.494169     60.200000    0.192094   \n",
       "1      31       0              270000      0.359378     48.000000    0.192094   \n",
       "2      23       0              398000      0.204553     60.200000    0.156100   \n",
       "3      19       1              300000      0.204560     53.875000    0.192094   \n",
       "4      25       0              378000      0.252571     35.000000    0.189800   \n",
       "...   ...     ...                 ...           ...           ...         ...   \n",
       "4995   32       0              458000      0.204559     53.000000    0.305604   \n",
       "4996   21       0              130000      0.358954     58.111111    0.192094   \n",
       "4997   24       0              589000      0.213772     70.571429    0.241720   \n",
       "4998   43       1              260000      0.359846     60.000000    0.228332   \n",
       "4999   20       1              139000      0.359367     60.200000    0.192094   \n",
       "\n",
       "      highcontact  deal default  \n",
       "0               0     0     NaN  \n",
       "1               1     1   False  \n",
       "2               1     0     NaN  \n",
       "3               0     1   False  \n",
       "4               1     1   False  \n",
       "...           ...   ...     ...  \n",
       "4995            1     0     NaN  \n",
       "4996            1     1    True  \n",
       "4997            1     0     NaN  \n",
       "4998            1     1   False  \n",
       "4999            1     0     NaN  \n",
       "\n",
       "[5000 rows x 9 columns]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['gender'] = data['gender'].astype(int)\n",
    "data['highcontact'] = data['highcontact'].astype(int)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "      <th>instalments_amount</th>\n",
       "      <th>nominalrates</th>\n",
       "      <th>tencentscore</th>\n",
       "      <th>gaodescore</th>\n",
       "      <th>highcontact</th>\n",
       "      <th>deal</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>5000.000000</td>\n",
       "      <td>5000.000000</td>\n",
       "      <td>5000.00000</td>\n",
       "      <td>4997.000000</td>\n",
       "      <td>5000.000000</td>\n",
       "      <td>5000.000000</td>\n",
       "      <td>5000.000000</td>\n",
       "      <td>5000.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>27.675400</td>\n",
       "      <td>0.146600</td>\n",
       "      <td>406201.42000</td>\n",
       "      <td>0.276058</td>\n",
       "      <td>58.608168</td>\n",
       "      <td>0.201975</td>\n",
       "      <td>0.492200</td>\n",
       "      <td>0.441400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>8.326146</td>\n",
       "      <td>0.353742</td>\n",
       "      <td>130623.36024</td>\n",
       "      <td>0.085912</td>\n",
       "      <td>14.218112</td>\n",
       "      <td>0.076724</td>\n",
       "      <td>0.499989</td>\n",
       "      <td>0.496604</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>18.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>50000.00000</td>\n",
       "      <td>0.130080</td>\n",
       "      <td>9.000000</td>\n",
       "      <td>0.023518</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>21.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>320000.00000</td>\n",
       "      <td>0.204560</td>\n",
       "      <td>53.888889</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>25.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>398000.00000</td>\n",
       "      <td>0.204579</td>\n",
       "      <td>60.200000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>32.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>498000.00000</td>\n",
       "      <td>0.359347</td>\n",
       "      <td>65.258929</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>56.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>869000.00000</td>\n",
       "      <td>0.494185</td>\n",
       "      <td>98.000000</td>\n",
       "      <td>0.732120</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               age       gender  instalments_amount  nominalrates  \\\n",
       "count  5000.000000  5000.000000          5000.00000   4997.000000   \n",
       "mean     27.675400     0.146600        406201.42000      0.276058   \n",
       "std       8.326146     0.353742        130623.36024      0.085912   \n",
       "min      18.000000     0.000000         50000.00000      0.130080   \n",
       "25%      21.000000     0.000000        320000.00000      0.204560   \n",
       "50%      25.000000     0.000000        398000.00000      0.204579   \n",
       "75%      32.000000     0.000000        498000.00000      0.359347   \n",
       "max      56.000000     1.000000        869000.00000      0.494185   \n",
       "\n",
       "       tencentscore   gaodescore  highcontact         deal  \n",
       "count   5000.000000  5000.000000  5000.000000  5000.000000  \n",
       "mean      58.608168     0.201975     0.492200     0.441400  \n",
       "std       14.218112     0.076724     0.499989     0.496604  \n",
       "min        9.000000     0.023518     0.000000     0.000000  \n",
       "25%       53.888889     0.192094     0.000000     0.000000  \n",
       "50%       60.200000     0.192094     0.000000     0.000000  \n",
       "75%       65.258929     0.192094     1.000000     1.000000  \n",
       "max       98.000000     0.732120     1.000000     1.000000  "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_5045/3007619741.py:4: UserWarning: Pandas requires version '3.0.5' or newer of 'xlsxwriter' (version '3.0.3' currently installed).\n",
      "  descriptive_stats.to_excel('outcome1.1.xlsx', index=False)\n"
     ]
    }
   ],
   "source": [
    "#描述性统计\n",
    "descriptive_stats = data[['age', 'gender','instalments_amount','nominalrates','tencentscore','gaodescore','highcontact','deal','default']].describe()\n",
    "descriptive_stats\n",
    "descriptive_stats.to_excel('outcome1.1.xlsx', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "      <th>instalments_amount</th>\n",
       "      <th>nominalrates</th>\n",
       "      <th>tencentscore</th>\n",
       "      <th>gaodescore</th>\n",
       "      <th>highcontact</th>\n",
       "      <th>deal</th>\n",
       "      <th>default</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>24</td>\n",
       "      <td>0</td>\n",
       "      <td>603000</td>\n",
       "      <td>0.494169</td>\n",
       "      <td>60.200000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "      <td>270000</td>\n",
       "      <td>0.359378</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23</td>\n",
       "      <td>0</td>\n",
       "      <td>398000</td>\n",
       "      <td>0.204553</td>\n",
       "      <td>60.200000</td>\n",
       "      <td>0.156100</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>19</td>\n",
       "      <td>1</td>\n",
       "      <td>300000</td>\n",
       "      <td>0.204560</td>\n",
       "      <td>53.875000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>25</td>\n",
       "      <td>0</td>\n",
       "      <td>378000</td>\n",
       "      <td>0.252571</td>\n",
       "      <td>35.000000</td>\n",
       "      <td>0.189800</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4995</th>\n",
       "      <td>32</td>\n",
       "      <td>0</td>\n",
       "      <td>458000</td>\n",
       "      <td>0.204559</td>\n",
       "      <td>53.000000</td>\n",
       "      <td>0.305604</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4996</th>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "      <td>130000</td>\n",
       "      <td>0.358954</td>\n",
       "      <td>58.111111</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4997</th>\n",
       "      <td>24</td>\n",
       "      <td>0</td>\n",
       "      <td>589000</td>\n",
       "      <td>0.213772</td>\n",
       "      <td>70.571429</td>\n",
       "      <td>0.241720</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4998</th>\n",
       "      <td>43</td>\n",
       "      <td>1</td>\n",
       "      <td>260000</td>\n",
       "      <td>0.359846</td>\n",
       "      <td>60.000000</td>\n",
       "      <td>0.228332</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4999</th>\n",
       "      <td>20</td>\n",
       "      <td>1</td>\n",
       "      <td>139000</td>\n",
       "      <td>0.359367</td>\n",
       "      <td>60.200000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4997 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      age  gender  instalments_amount  nominalrates  tencentscore  gaodescore  \\\n",
       "0      24       0              603000      0.494169     60.200000    0.192094   \n",
       "1      31       0              270000      0.359378     48.000000    0.192094   \n",
       "2      23       0              398000      0.204553     60.200000    0.156100   \n",
       "3      19       1              300000      0.204560     53.875000    0.192094   \n",
       "4      25       0              378000      0.252571     35.000000    0.189800   \n",
       "...   ...     ...                 ...           ...           ...         ...   \n",
       "4995   32       0              458000      0.204559     53.000000    0.305604   \n",
       "4996   21       0              130000      0.358954     58.111111    0.192094   \n",
       "4997   24       0              589000      0.213772     70.571429    0.241720   \n",
       "4998   43       1              260000      0.359846     60.000000    0.228332   \n",
       "4999   20       1              139000      0.359367     60.200000    0.192094   \n",
       "\n",
       "      highcontact  deal default  \n",
       "0               0     0     NaN  \n",
       "1               1     1   False  \n",
       "2               1     0     NaN  \n",
       "3               0     1   False  \n",
       "4               1     1   False  \n",
       "...           ...   ...     ...  \n",
       "4995            1     0     NaN  \n",
       "4996            1     1    True  \n",
       "4997            1     0     NaN  \n",
       "4998            1     1   False  \n",
       "4999            1     0     NaN  \n",
       "\n",
       "[4997 rows x 9 columns]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=data.dropna(subset=['nominalrates'])\n",
    "data\n",
    "#对因变量不是default的情况，用data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_5045/4061617974.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  reg_data['default'] = reg_data['default'].astype(int)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "age                     int64\n",
       "gender                  int64\n",
       "instalments_amount      int64\n",
       "nominalrates          float64\n",
       "tencentscore          float64\n",
       "gaodescore            float64\n",
       "highcontact             int64\n",
       "deal                    int64\n",
       "default                 int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "      <th>instalments_amount</th>\n",
       "      <th>nominalrates</th>\n",
       "      <th>tencentscore</th>\n",
       "      <th>gaodescore</th>\n",
       "      <th>highcontact</th>\n",
       "      <th>deal</th>\n",
       "      <th>default</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>31</td>\n",
       "      <td>0</td>\n",
       "      <td>270000</td>\n",
       "      <td>0.359378</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>19</td>\n",
       "      <td>1</td>\n",
       "      <td>300000</td>\n",
       "      <td>0.204560</td>\n",
       "      <td>53.875000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>25</td>\n",
       "      <td>0</td>\n",
       "      <td>378000</td>\n",
       "      <td>0.252571</td>\n",
       "      <td>35.000000</td>\n",
       "      <td>0.189800</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "      <td>400000</td>\n",
       "      <td>0.359360</td>\n",
       "      <td>84.000000</td>\n",
       "      <td>0.187895</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "      <td>458000</td>\n",
       "      <td>0.204559</td>\n",
       "      <td>39.000000</td>\n",
       "      <td>0.072399</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4992</th>\n",
       "      <td>28</td>\n",
       "      <td>0</td>\n",
       "      <td>499000</td>\n",
       "      <td>0.358958</td>\n",
       "      <td>16.000000</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4993</th>\n",
       "      <td>38</td>\n",
       "      <td>0</td>\n",
       "      <td>398000</td>\n",
       "      <td>0.358955</td>\n",
       "      <td>41.444444</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4994</th>\n",
       "      <td>33</td>\n",
       "      <td>0</td>\n",
       "      <td>397000</td>\n",
       "      <td>0.204564</td>\n",
       "      <td>13.000000</td>\n",
       "      <td>0.214644</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4996</th>\n",
       "      <td>21</td>\n",
       "      <td>0</td>\n",
       "      <td>130000</td>\n",
       "      <td>0.358954</td>\n",
       "      <td>58.111111</td>\n",
       "      <td>0.192094</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4998</th>\n",
       "      <td>43</td>\n",
       "      <td>1</td>\n",
       "      <td>260000</td>\n",
       "      <td>0.359846</td>\n",
       "      <td>60.000000</td>\n",
       "      <td>0.228332</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2203 rows × 9 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      age  gender  instalments_amount  nominalrates  tencentscore  gaodescore  \\\n",
       "1      31       0              270000      0.359378     48.000000    0.192094   \n",
       "3      19       1              300000      0.204560     53.875000    0.192094   \n",
       "4      25       0              378000      0.252571     35.000000    0.189800   \n",
       "5      21       0              400000      0.359360     84.000000    0.187895   \n",
       "6      21       0              458000      0.204559     39.000000    0.072399   \n",
       "...   ...     ...                 ...           ...           ...         ...   \n",
       "4992   28       0              499000      0.358958     16.000000    0.192094   \n",
       "4993   38       0              398000      0.358955     41.444444    0.192094   \n",
       "4994   33       0              397000      0.204564     13.000000    0.214644   \n",
       "4996   21       0              130000      0.358954     58.111111    0.192094   \n",
       "4998   43       1              260000      0.359846     60.000000    0.228332   \n",
       "\n",
       "      highcontact  deal  default  \n",
       "1               1     1        0  \n",
       "3               0     1        0  \n",
       "4               1     1        0  \n",
       "5               0     1        0  \n",
       "6               0     0        0  \n",
       "...           ...   ...      ...  \n",
       "4992            1     1        1  \n",
       "4993            1     1        0  \n",
       "4994            1     1        0  \n",
       "4996            1     1        1  \n",
       "4998            1     1        0  \n",
       "\n",
       "[2203 rows x 9 columns]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#因为default这一行的数据很特殊，它有缺失值，并且很大，但是数据是很宝贵的，为此本文专门对default数据进行特殊处理\n",
    "reg_data=data.dropna(subset=['default'])\n",
    "reg_data['default'] = reg_data['default'].astype(int)\n",
    "reg_data.dtypes\n",
    "reg_data\n",
    "#对因变量是逾期，自变量不包括买家信用的，用reg_data,即第二问第二、三次回归\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_5045/776674217.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  reg_data['default'] = reg_data['default'].astype(int)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>default</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>2203.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.419882</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.493651</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           default\n",
       "count  2203.000000\n",
       "mean      0.419882\n",
       "std       0.493651\n",
       "min       0.000000\n",
       "25%       0.000000\n",
       "50%       0.000000\n",
       "75%       1.000000\n",
       "max       1.000000"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_5045/776674217.py:4: UserWarning: Pandas requires version '3.0.5' or newer of 'xlsxwriter' (version '3.0.3' currently installed).\n",
      "  descriptive_stats2.to_excel('outcome1.2.xlsx', index=False)\n"
     ]
    }
   ],
   "source": [
    "reg_data['default'] = reg_data['default'].astype(int)\n",
    "descriptive_stats2 = reg_data[['default']].describe()\n",
    "descriptive_stats2\n",
    "descriptive_stats2.to_excel('outcome1.2.xlsx', index=False)\n",
    "#第二问第一次回归"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "问题2:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.677831\n",
      "         Iterations 4\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                default   No. Observations:                 2203\n",
      "Model:                          Logit   Df Residuals:                     2201\n",
      "Method:                           MLE   Df Model:                            1\n",
      "Date:                Thu, 19 Dec 2024   Pseudo R-squ.:                0.003561\n",
      "Time:                        17:36:41   Log-Likelihood:                -1493.3\n",
      "converged:                       True   LL-Null:                       -1498.6\n",
      "Covariance Type:            nonrobust   LLR p-value:                  0.001087\n",
      "================================================================================\n",
      "                   coef    std err          z      P>|z|      [0.025      0.975]\n",
      "--------------------------------------------------------------------------------\n",
      "const           -0.8265      0.161     -5.123      0.000      -1.143      -0.510\n",
      "tencentscore     0.0091      0.003      3.250      0.001       0.004       0.015\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "X1 = sm.add_constant(reg_data['tencentscore'])  # 添加常数项到自变量\n",
    "y1 = reg_data['default']  # 因变量\n",
    "logit_model1 = sm.Logit(y1, X1)\n",
    "result1 = logit_model1.fit()\n",
    "print(result1.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.675350\n",
      "         Iterations 4\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                default   No. Observations:                 2203\n",
      "Model:                          Logit   Df Residuals:                     2198\n",
      "Method:                           MLE   Df Model:                            4\n",
      "Date:                Thu, 19 Dec 2024   Pseudo R-squ.:                0.007210\n",
      "Time:                        17:36:41   Log-Likelihood:                -1487.8\n",
      "converged:                       True   LL-Null:                       -1498.6\n",
      "Covariance Type:            nonrobust   LLR p-value:                 0.0002398\n",
      "================================================================================\n",
      "                   coef    std err          z      P>|z|      [0.025      0.975]\n",
      "--------------------------------------------------------------------------------\n",
      "const           -4.3267      1.646     -2.629      0.009      -7.552      -1.101\n",
      "tencentscore     0.0095      0.003      3.361      0.001       0.004       0.015\n",
      "gender          -0.0016      0.114     -0.014      0.989      -0.224       0.221\n",
      "ln_amount        0.2386      0.126      1.896      0.058      -0.008       0.485\n",
      "nominalrates     1.4636      0.537      2.727      0.006       0.412       2.516\n",
      "================================================================================\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_5045/1803321995.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  reg_data['ln_amount'] = np.log(reg_data['instalments_amount'])\n"
     ]
    }
   ],
   "source": [
    "reg_data['ln_amount'] = np.log(reg_data['instalments_amount'])\n",
    "# 定义自变量（X）和因变量（y）\n",
    "X1_1 = reg_data[['tencentscore', 'gender', 'ln_amount','nominalrates']]\n",
    "y1_1 = reg_data['default']\n",
    "# 添加常数项以拟合截距\n",
    "X1_1 = sm.add_constant(X1_1)\n",
    "# 定义Logit模型\n",
    "logit_model = sm.Logit(y1_1, X1_1)\n",
    "# 拟合模型\n",
    "result = logit_model.fit()\n",
    "# 查看模型结果\n",
    "print(result.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.677694\n",
      "         Iterations 4\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                default   No. Observations:                 2203\n",
      "Model:                          Logit   Df Residuals:                     2201\n",
      "Method:                           MLE   Df Model:                            1\n",
      "Date:                Thu, 19 Dec 2024   Pseudo R-squ.:                0.003763\n",
      "Time:                        17:36:41   Log-Likelihood:                -1493.0\n",
      "converged:                       True   LL-Null:                       -1498.6\n",
      "Covariance Type:            nonrobust   LLR p-value:                 0.0007836\n",
      "==============================================================================\n",
      "                 coef    std err          z      P>|z|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const         -0.7071      0.123     -5.748      0.000      -0.948      -0.466\n",
      "gaodescore     1.9828      0.593      3.342      0.001       0.820       3.146\n",
      "==============================================================================\n"
     ]
    }
   ],
   "source": [
    "X2 = sm.add_constant(reg_data['gaodescore'])  # 添加常数项到自变量\n",
    "y1 = reg_data['default']  # 因变量\n",
    "logit_model1 = sm.Logit(y1, X2)\n",
    "result2 = logit_model1.fit()\n",
    "print(result2.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.675476\n",
      "         Iterations 4\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                default   No. Observations:                 2203\n",
      "Model:                          Logit   Df Residuals:                     2198\n",
      "Method:                           MLE   Df Model:                            4\n",
      "Date:                Thu, 19 Dec 2024   Pseudo R-squ.:                0.007023\n",
      "Time:                        17:36:41   Log-Likelihood:                -1488.1\n",
      "converged:                       True   LL-Null:                       -1498.6\n",
      "Covariance Type:            nonrobust   LLR p-value:                 0.0003095\n",
      "================================================================================\n",
      "                   coef    std err          z      P>|z|      [0.025      0.975]\n",
      "--------------------------------------------------------------------------------\n",
      "const           -3.8689      1.624     -2.382      0.017      -7.052      -0.685\n",
      "gaodescore       1.9552      0.596      3.281      0.001       0.787       3.123\n",
      "gender           0.0396      0.113      0.349      0.727      -0.183       0.262\n",
      "ln_amount        0.2153      0.125      1.719      0.086      -0.030       0.461\n",
      "nominalrates     1.3985      0.537      2.604      0.009       0.346       2.451\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "X2_1 = reg_data[['gaodescore', 'gender', 'ln_amount','nominalrates']]\n",
    "y2_1 = reg_data['default']\n",
    "X2_1 = sm.add_constant(X2_1)\n",
    "logit_model = sm.Logit(y2_1, X2_1)\n",
    "result = logit_model.fit()\n",
    "print(result.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "问题三"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.663147\n",
      "         Iterations 5\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                   deal   No. Observations:                 4997\n",
      "Model:                          Logit   Df Residuals:                     4995\n",
      "Method:                           MLE   Df Model:                            1\n",
      "Date:                Thu, 19 Dec 2024   Pseudo R-squ.:                 0.03364\n",
      "Time:                        17:36:41   Log-Likelihood:                -3313.7\n",
      "converged:                       True   LL-Null:                       -3429.1\n",
      "Covariance Type:            nonrobust   LLR p-value:                 4.170e-52\n",
      "================================================================================\n",
      "                   coef    std err          z      P>|z|      [0.025      0.975]\n",
      "--------------------------------------------------------------------------------\n",
      "const            1.6089      0.130     12.412      0.000       1.355       1.863\n",
      "tencentscore    -0.0316      0.002    -14.585      0.000      -0.036      -0.027\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "X3 = sm.add_constant(data['tencentscore'])  # 添加常数项到自变量\n",
    "y2 = data['deal']  # 因变量\n",
    "logit_model3 = sm.Logit(y2, X3)\n",
    "result3 = logit_model3.fit()\n",
    "print(result3.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/_p/rfbd33cd5jggpfbv91zgdptr0000gn/T/ipykernel_5045/3864852005.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  data['ln_amount'] = np.log(data['instalments_amount'])\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.646681\n",
      "         Iterations 5\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                   deal   No. Observations:                 4997\n",
      "Model:                          Logit   Df Residuals:                     4992\n",
      "Method:                           MLE   Df Model:                            4\n",
      "Date:                Thu, 19 Dec 2024   Pseudo R-squ.:                 0.05763\n",
      "Time:                        17:36:41   Log-Likelihood:                -3231.5\n",
      "converged:                       True   LL-Null:                       -3429.1\n",
      "Covariance Type:            nonrobust   LLR p-value:                 2.930e-84\n",
      "================================================================================\n",
      "                   coef    std err          z      P>|z|      [0.025      0.975]\n",
      "--------------------------------------------------------------------------------\n",
      "const            8.2179      1.102      7.458      0.000       6.058      10.378\n",
      "tencentscore    -0.0332      0.002    -15.024      0.000      -0.038      -0.029\n",
      "gender           0.4823      0.084      5.772      0.000       0.318       0.646\n",
      "ln_amount       -0.5803      0.084     -6.900      0.000      -0.745      -0.416\n",
      "nominalrates     3.1476      0.345      9.118      0.000       2.471       3.824\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "data['ln_amount'] = np.log(data['instalments_amount'])\n",
    "X3_1 = data[['tencentscore', 'gender', 'ln_amount','nominalrates']]\n",
    "y3_1 = data['deal']\n",
    "X3_1 = sm.add_constant(X3_1)\n",
    "logit_model = sm.Logit(y3_1, X3_1)\n",
    "result = logit_model.fit()\n",
    "print(result.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.680475\n",
      "         Iterations 5\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                   deal   No. Observations:                 4997\n",
      "Model:                          Logit   Df Residuals:                     4995\n",
      "Method:                           MLE   Df Model:                            1\n",
      "Date:                Thu, 19 Dec 2024   Pseudo R-squ.:                0.008388\n",
      "Time:                        17:36:41   Log-Likelihood:                -3400.3\n",
      "converged:                       True   LL-Null:                       -3429.1\n",
      "Covariance Type:            nonrobust   LLR p-value:                 3.331e-14\n",
      "==============================================================================\n",
      "                 coef    std err          z      P>|z|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const          0.3518      0.084      4.176      0.000       0.187       0.517\n",
      "gaodescore    -2.9315      0.399     -7.353      0.000      -3.713      -2.150\n",
      "==============================================================================\n"
     ]
    }
   ],
   "source": [
    "X4 = sm.add_constant(data['gaodescore'])  # 添加常数项到自变量\n",
    "y2 = data['deal']  # 因变量\n",
    "logit_model4 = sm.Logit(y2, X4)\n",
    "result4 = logit_model4.fit()\n",
    "print(result4.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.666047\n",
      "         Iterations 5\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                   deal   No. Observations:                 4997\n",
      "Model:                          Logit   Df Residuals:                     4992\n",
      "Method:                           MLE   Df Model:                            4\n",
      "Date:                Thu, 19 Dec 2024   Pseudo R-squ.:                 0.02941\n",
      "Time:                        17:36:41   Log-Likelihood:                -3328.2\n",
      "converged:                       True   LL-Null:                       -3429.1\n",
      "Covariance Type:            nonrobust   LLR p-value:                 1.600e-42\n",
      "================================================================================\n",
      "                   coef    std err          z      P>|z|      [0.025      0.975]\n",
      "--------------------------------------------------------------------------------\n",
      "const            5.7604      1.061      5.428      0.000       3.680       7.841\n",
      "gaodescore      -2.8402      0.406     -7.000      0.000      -3.635      -2.045\n",
      "gender           0.4190      0.082      5.121      0.000       0.259       0.579\n",
      "ln_amount       -0.4927      0.082     -6.008      0.000      -0.653      -0.332\n",
      "nominalrates     3.0468      0.338      9.005      0.000       2.384       3.710\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "X4_1 = data[['gaodescore', 'gender', 'ln_amount','nominalrates']]\n",
    "y4_1 = data['deal']\n",
    "X4_1 = sm.add_constant(X4_1)\n",
    "logit_model = sm.Logit(y4_1, X4_1)\n",
    "result = logit_model.fit()\n",
    "print(result.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "问题4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.685351\n",
      "         Iterations 4\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                   deal   No. Observations:                 4997\n",
      "Model:                          Logit   Df Residuals:                     4995\n",
      "Method:                           MLE   Df Model:                            1\n",
      "Date:                Thu, 19 Dec 2024   Pseudo R-squ.:                0.001283\n",
      "Time:                        17:36:41   Log-Likelihood:                -3424.7\n",
      "converged:                       True   LL-Null:                       -3429.1\n",
      "Covariance Type:            nonrobust   LLR p-value:                  0.003014\n",
      "===============================================================================\n",
      "                  coef    std err          z      P>|z|      [0.025      0.975]\n",
      "-------------------------------------------------------------------------------\n",
      "const          -0.3197      0.040     -7.949      0.000      -0.399      -0.241\n",
      "highcontact     0.1691      0.057      2.965      0.003       0.057       0.281\n",
      "===============================================================================\n"
     ]
    }
   ],
   "source": [
    "X5 = sm.add_constant(data['highcontact'])  # 添加常数项到自变量\n",
    "y3 = data['deal']  # 因变量\n",
    "logit_model5 = sm.Logit(y3, X5)\n",
    "result5 = logit_model5.fit()\n",
    "print(result5.summary())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Optimization terminated successfully.\n",
      "         Current function value: 0.670371\n",
      "         Iterations 4\n",
      "                           Logit Regression Results                           \n",
      "==============================================================================\n",
      "Dep. Variable:                   deal   No. Observations:                 4997\n",
      "Model:                          Logit   Df Residuals:                     4992\n",
      "Method:                           MLE   Df Model:                            4\n",
      "Date:                Thu, 19 Dec 2024   Pseudo R-squ.:                 0.02311\n",
      "Time:                        17:36:41   Log-Likelihood:                -3349.8\n",
      "converged:                       True   LL-Null:                       -3429.1\n",
      "Covariance Type:            nonrobust   LLR p-value:                 3.050e-33\n",
      "================================================================================\n",
      "                   coef    std err          z      P>|z|      [0.025      0.975]\n",
      "--------------------------------------------------------------------------------\n",
      "const            5.0351      1.051      4.791      0.000       2.975       7.095\n",
      "highcontact      0.1719      0.058      2.967      0.003       0.058       0.285\n",
      "gender           0.4395      0.082      5.389      0.000       0.280       0.599\n",
      "ln_amount       -0.4883      0.082     -5.983      0.000      -0.648      -0.328\n",
      "nominalrates     3.0890      0.337      9.156      0.000       2.428       3.750\n",
      "================================================================================\n"
     ]
    }
   ],
   "source": [
    "X6_1 = data[['highcontact', 'gender', 'ln_amount','nominalrates']]\n",
    "y6_1 = data['deal']\n",
    "X6_1 = sm.add_constant(X6_1)\n",
    "logit_model = sm.Logit(y6_1, X6_1)\n",
    "result = logit_model.fit()\n",
    "print(result.summary())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
